import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.net.URISyntaxException;

public class 测试反汇编器 {

    private void 测试(String 文件名, String 结尾字符串) throws IOException, URISyntaxException {
        var 源码 = Tools.读文件(Tools.getResourcePath(文件名));
        var 汇编器 = 字节码汇编器.汇编(源码);
        var 字节码输出 = 字节码汇编器.输出字节码(汇编器.字节码, 汇编器.取字节码尺寸());
        System.out.println("字节码:");
        System.out.println(字节码输出);

        System.out.println("反汇编：");
        var 反汇编 = new 反汇编器(汇编器.字节码, 汇编器.取字节码尺寸(), 汇编器.常量池.toArray());
        反汇编.反汇编();
        System.out.println(反汇编.反汇编输出);
        Assert.assertTrue(反汇编.反汇编输出.toString().endsWith(结尾字符串));
    }

    @Test
    public void 测试T0() throws IOException, URISyntaxException {
        测试("t.rcode", "0050: \thalt       ");
    }

    @Test
    public void 测试T2() throws IOException, URISyntaxException {
        测试("t2.rcode", "0072: \thalt       ");
    }

    @Test
    public void 测试T3() throws IOException, URISyntaxException {
        测试("t3.rcode", "0065: \thalt       ");
    }

    @Test
    public void 测试T4() throws IOException, URISyntaxException {
        测试("t4.rcode", "0078: \tret        ");
    }

    @Test
    public void 测试Struct() throws IOException, URISyntaxException {
        测试("struct.rcode", "0094: \thalt       ");
    }
}
